package com.vc.tools;

import android.content.Context;
import android.net.wifi.WifiManager;

public class VideoWifiLock {
	
	private Context context = null;
	private int lockType = 0;
	private String lockTag = null;
	WifiManager.WifiLock _wifiLock = null;
	
	public VideoWifiLock(Context c, int type, String tag) {
		context = c;
		lockType = type;
		lockTag = tag;
	}
	
	public boolean lock(){
		boolean ret = false;
		
		if (_wifiLock == null){
			WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			_wifiLock = wifiManager.createWifiLock(lockType, lockTag);
		}
		
		if (_wifiLock != null){
			if (!_wifiLock.isHeld()){
				_wifiLock.acquire();	
			}
			ret = true;
		}
		
		return ret;
	}
	
	public void unlock(){
		if (isLocked()){
			_wifiLock.release();
		}
	}
	
	public boolean isLocked(){
		return (_wifiLock != null && _wifiLock.isHeld());
	}

}
